<?php

namespace app\serversms\service\sms;
use app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-06-26 11:01:39
 * @Description: 手机短信的配置信息 （模板信息 apikey等）
 */

class Sms extends ApiBase
{
	
	protected $smsLogic = '\app\serversms\logic\Sms';
    protected $smsValidate = '\app\serversms\validate\Sms';

    use \app\serversms\traits\BaseCheckApp;
    /*添加一个短信配置模板（模板+apikey等）*/
    public function addSms(): array 
    {
    	$data = $this->receiveData(['app_id'=>0,'other_app_read'=>'','is_public'=>0,'action'=>' ','description'=>'','sms_info'=>[]]);
    	return $this->_updateSms($data, false);
    }

    /*编辑某个短信配置*/
    public function editSms(): array 
    {
    	$data = $this->issetReceiveData(['id'=>0,'app_id'=>0,'other_app_read'=>'','is_public'=>0,'action'=>' ','description'=>'','sms_info'=>[]]);
    	return $this->_updateSms($data, true);
    }

    protected function _updateSms($data, bool $isEdit): array 
    {
    	$scene = $isEdit ? 'edit' : '';
        //接收数据的验证
    	$validate = validate($this->smsValidate);
        if(!$validate->scene($scene)->check($data)){ return verify_err(null, $validate->getError()); }
        if(isset($data['sms_info']) && !$validate->scene('smsInfo')->check($data['sms_info'])){ 
        	return verify_err(null, $validate->getError()); 
        }
        //对所属应用下的记录做基本验证
        $checkApp = $this->_baseCheckApp($data, '', 'edit', $this->smsValidate, $this->smsLogic, 'dbOneInfo');
        if($checkApp['status'] != SUCCESS_STATUS){
            return $checkApp;
        }

        if($isEdit){
        	return $this->logic($this->smsLogic)->editSms($data);
        }else{
        	return $this->logic($this->smsLogic)->addSms($data);
        }
    }

    /*获取某应用下的所有短信模板配置*/
    public function smsList(): array 
    {
    	$data = $this->issetReceiveData(['app_id'=>0,'page'=>1,'listRows'=>15]);
    	return $this->stepRun($data, 'smsList', 'smsList');
    }

    /*获取某个短信配置详情*/
    public function smsInfo(): array 
    {
    	$data = $this->issetReceiveData(['app_id'=>0,'id'=>0,'action'=>'']);
        if(!isset($data['id']) && !isset($data['action'])){
            return verify_err(null, 'id和action两者必须传一个');
        }
    	return $this->stepRun($data, 'smsInfo', 'getSmsInfo');
    }

    /*删除某短信配置*/
    public function delOneSms(): array 
    {
    	$data = $this->receiveData(['app_id'=>0,'id'=>0]);
    	return $this->stepRun($data, 'delOneSms');
    }

    /*发送单条短信*/
    public function sendOne(): array 
    {
    	$data = $this->receiveData(['send_info'=>[]]);

        $validate = validate($this->smsValidate);
        if(!$validate->scene('sendInfo')->check($data['send_info'])){ 
            return verify_err(null, $validate->getError()); 
        }

        $data['smsName'] = config('smsName');
        if($data['smsName']){
            return $this->logic($this->smsLogic)->sendOne($data);
        }else{
            return auto_failure(null, '未配置发送短信的方式');
        }
    }

    /*群发短信*/
    public function batchSend(): array 
    {
    	$data = $this->receiveData(['send_info'=>[]]);

        $validate = validate($this->smsValidate);
        if(!$validate->scene('sendInfos')->check($data['send_info'])){ 
            return verify_err(null, $validate->getError()); 
        }

        $data['smsName'] = config('smsName');
        if($data['smsName']){
            return $this->logic($this->smsLogic)->batchSend($data);
        }else{
            return auto_failure(null, '未配置发送短信的方式');
        }
    }


}






